package com.uobgroup.ews.uam.core.domain;

import com.uobgroup.ews.uam.core.dao.AccessControlDao;
import com.uobgroup.ews.uam.core.service.AccessControlService;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;

@Configurable
@Component
public class AccessControlDataOnDemand {

	private Random rnd = new SecureRandom();

	private List<AccessControl> data;

	@Autowired
    AccessControlService accessControlService;

	@Autowired
    AccessControlDao accessControlDao;

	public AccessControl getNewTransientAccessControl(int index) {
        AccessControl obj = new AccessControl();
        setDescription(obj, index);
        setName(obj, index);
        setPermission(obj, index);
        obj.setType(CombinationType.CASE);
        return obj;
    }

	public void setDescription(AccessControl obj, int index) {
        String description = "description_" + index;
        if (description.length() > 1000) {
            description = description.substring(0, 1000);
        }
        obj.setDescription(description);
    }

	public void setName(AccessControl obj, int index) {
        String name = "name_" + index;
        if (name.length() > 255) {
            name = name.substring(0, 255);
        }
        obj.setName(name);
    }

	public void setPermission(AccessControl obj, int index) {
        Integer permission = new Integer(index);
        obj.setPermission(permission);
    }

	public AccessControl getSpecificAccessControl(int index) {
        init();
        if (index < 0) {
            index = 0;
        }
        if (index > (data.size() - 1)) {
            index = data.size() - 1;
        }
        AccessControl obj = data.get(index);
        Long id = obj.getId();
        return accessControlService.findAccessControl(id);
    }

	public AccessControl getRandomAccessControl() {
        init();
        AccessControl obj = data.get(rnd.nextInt(data.size()));
        Long id = obj.getId();
        return accessControlService.findAccessControl(id);
    }

	public boolean modifyAccessControl(AccessControl obj) {
        return false;
    }

	public void init() {
        int from = 0;
        int to = 10;
        data = accessControlService.findAccessControlEntries(from, to);
        if (data == null) {
            throw new IllegalStateException("Find entries implementation for 'AccessControl' illegally returned null");
        }
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<AccessControl>();
        for (int i = 0; i < 10; i++) {
            AccessControl obj = getNewTransientAccessControl(i);
            try {
                accessControlService.saveAccessControl(obj);
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> iter = e.getConstraintViolations().iterator(); iter.hasNext();) {
                    ConstraintViolation<?> cv = iter.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            accessControlDao.flush();
            data.add(obj);
        }
    }
}
